package com.eastcom.bm.dataprocess.noticeserver;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class ChannelManager {

	private Map<String,NoticeChannel> noticeChannelPool=new HashMap<String,NoticeChannel>();
	
	private Map <String,Class>channelclazzLst;
	
	private static ChannelManager instance=null;
	
	public static ChannelManager getInstance(){
		if(instance==null)
			instance=new ChannelManager();
		
		return instance;
	}
	
	public ChannelManager(){
	}
	
    public ChannelManager(Map <String,Class>channelclazzLst) throws Exception{
		this.channelclazzLst=channelclazzLst;
		init();
	}
	
	public void init() throws Exception{
		Iterator it=channelclazzLst.entrySet().iterator();
		while(it.hasNext()){
			Entry entry=(Entry) it.next();
			Constructor constructor=((Class)entry.getValue()).getConstructor(new Class[]{String.class});
			NoticeChannel nc=(NoticeChannel) constructor.newInstance(new Object[]{"Alarm Message Task"});
			noticeChannelPool.put((String)entry.getKey(), nc);
		}
	}
	
	public NoticeChannel getNoticeChannel(String type){
		return noticeChannelPool.get(type);
	}
	
	public void addChannel(String type,Class clazz) throws Exception{
		NoticeChannel nc=(NoticeChannel)clazz.newInstance();
		noticeChannelPool.put(type, nc);
	}
	
	public void removeChannel(String type){
		noticeChannelPool.remove(type);
	}
	
}
